package org.social.manager;

import com.google.common.collect.Lists;

import org.bean.NoticeDTO;
import org.social.db.bean.Notice;
import org.social.db.cache.NoticeCache;
import org.utils.RemoteUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by fxf on 2018-1-9.
 */
public class NoticeManager {

    public static void addNotice(Notice notice) {
        NoticeCache.me().insert(notice);
        List<Notice> list = Lists.newArrayList();
        list.add(notice);
        topicGameServerAddNotice(list);
    }

    public static void deleteNotice(List<Integer> idList, boolean delDb) {
        for (Integer id : idList) {
            Notice notice = NoticeCache.me().getById(id);
            if (notice != null) {
                if (delDb) {
                    NoticeCache.me().delete(notice);
                }
            }
        }
        topicGameServerDeleteNotice(idList);
    }

    public static void modifyNotice(Notice n) {
        Notice notice = NoticeCache.me().getById(n.getId());
        if (notice != null) {
            NoticeCache.me().update(notice);
            List<Notice> list = Lists.newArrayList();
            list.add(notice);
            topicGameServerAddNotice(list);
        }
    }

    public static void topicGameServerAddNotice(List<Notice> noticeList) {
        List<NoticeDTO> noticeDTOList =
                noticeList.stream().map(notice -> buildNoticeDTO(notice))
                        .collect(Collectors.toList());
        RemoteUtil.gameTopic().addNotice(noticeDTOList);
    }

    public static void topicGameServerDeleteNotice(List<Integer> noticeIdList) {
        RemoteUtil.gameTopic().removeNotice(noticeIdList);
    }

    /** 通知游戏服 */
    public static void notifyRemote(String serverId) {
        long nowTime = System.currentTimeMillis();
        List<Notice> noticeList = NoticeCache.me().loadAll();
        List<NoticeDTO> noticeDTOList =
                noticeList.stream().filter(notice -> notice.getEndTime().getTime() >= nowTime)
                        .map(notice -> buildNoticeDTO(notice)).collect(Collectors.toList());
        RemoteUtil.gameAsyn(serverId).noticeNotice(noticeDTOList);
    }

    public static Notice getNotice(int id) {
        Notice notice = NoticeCache.me().getById(id);
        return notice;
    }

    public static List<Notice> getNotice(boolean history) {
        long nowTime = System.currentTimeMillis();
        List<Notice> noticeList = NoticeCache.me().loadAll();
        return noticeList
                .stream()
                .filter(notice -> history ? notice.getEndTime().getTime() <= nowTime : notice
                        .getEndTime().getTime() >= nowTime).collect(Collectors.toList());
    }

    private static NoticeDTO buildNoticeDTO(Notice notice) {
        NoticeDTO dto = new NoticeDTO();
        return dto.setId(notice.getId()).setChannelAppId(notice.getChannelAppId())
                .setChannel(notice.getChannel()).setIdx(notice.getIdx()).setType(notice.getType())
                .setTag(notice.getTag()).setTitle(notice.getTitle())
                .setContent(notice.getContent()).setParam(notice.getParam())
                .setStartTime(notice.getStartTime()).setEndTime(notice.getEndTime())
                .setContextImg(notice.getContextImg());
    }
}
